package launcher;

import static java.lang.System.in;
import static java.lang.System.out;

import java.util.Scanner;

import queries.Document;
import queries.Query;
import queries.QueryResult;
import queries.YahooQueryExecutor;
import util.Logger;

/**
 * Main command-line interface for Relevance Feedback System
 *
 *  @author ak2765
 * @author Vika
 *
 */
public class CommandLine {
	
	static boolean DEBUG = false;

	static Scanner scanner = new Scanner(in);
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		out.println("COMS E6111 Advanced Database Systems - Professor Gravano - Homework1");
		out.println("Students: Antonio Kantek (ak2765) and Vika K. (vk2185)");
		
		out.println("Please enter the list of words (separated by spaces) that represents your query");
		String[] listOfWords = scanner.nextLine().split(" ");
		
		out.println("Now please enter the precision (from 0 to 1): ");
		double precision = scanner.nextDouble();
		
		YahooQueryExecutor executor = new YahooQueryExecutor(new Query(listOfWords, precision));
		
		QueryResult result = executor.executeInitialRound();
	
		int round = 1;
		
		Logger.info("Initial result:");
		displayQueryResult(result);		
		if(result.getDocuments().size() < 10){
			Logger.info("Please revise your query");
			return;
		}
		
		int[] selectedDocuments = selectFavoriteDocuments(result);;
		//while (isNotSatisfactory(result)) {
		while (precision > selectedDocuments.length/10.0){
			
			Logger.info("Precision " + selectedDocuments.length/10.0);
			Logger.info("Required precision " + precision);
			
			// Exit if there are no relevant results returned
			if(selectedDocuments.length == 0){
				Logger.info("Sorry about that :(");
				break;
			}
			
			round++;
			
			result = executor.executeNextRound(selectedDocuments);
			
			Logger.info("Round (" + round + ") result:");
			displayQueryResult(result);	
			
			if (DEBUG) {
			Logger.info("BiGrams: " + executor.getQueryRefiner().getBiGrams());
			}
			
			selectedDocuments = selectFavoriteDocuments(result);
			
		} 
		
		Logger.info("Good-bye!");
	}

	/**
	 * Query relevance feedback from user
	 * 
	 * @param result
	 * @return
	 */
	private static int[] selectFavoriteDocuments(QueryResult result) {
		out.println("Please enter the values (separated by space) of the results you liked more");
		out.println("Enter 0 if nothing is relevant\n");
		
		// skip blank lines
		String line;
		do {
			line = scanner.nextLine().trim();
		} while (line.equals(""));
		
		String[] selectedValuesString = line.split(" ");
		
		int[] selectedValues = new int[selectedValuesString.length];

		for(int i=0;i<selectedValuesString.length;i++) {
			if(selectedValuesString[i].equals("0")){
				return new int[0];
			} 
			selectedValues[i] = Integer.parseInt(selectedValuesString[i]) - 1;
		}
		
		return selectedValues;
	}

	/**
	 * @param result
	 * @return
	 */
	private static boolean isNotSatisfactory(QueryResult result) {
		out.println("Do you want the computer to refine your query (y/n) ?");

		Boolean finalAnswer = null;
		do {
			String answer = scanner.nextLine();
			if ("y".equals(answer)) {
				finalAnswer = true;
			}
			if ("n".equals(answer)) {
				finalAnswer = false;
			}
			
			out.println("Please enter [y] or [n]");
			
		} while (finalAnswer == null);
		
		return finalAnswer;
	}
	
	/**
	 * Display query result to console
	 * 
	 * @param result
	 */
	private static void displayQueryResult(QueryResult result) {
		int option = 1;
		for(Document doc : result.getDocuments()) {
			out.println("[" + option + "] - " + printPretty(doc) );
			option++;
		}
	}

	/**
	 * @param doc
	 * @return
	 */
	private static String printPretty(Document doc) {
		StringBuilder buff = new StringBuilder();
		buff.append("\ttitle: [").append(doc.getTitle()).append("]");
		buff.append("\n\tdescription: [").append(doc.getDescription()).append("]");
		buff.append("\n\turl: [").append(doc.getUrl()).append("]\n");
		return buff.toString();
	}

}
